package cn.hackcloud.javabase.agent;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface Subject {
	void action();
}

class RealSubject implements Subject {

	@Override
	public void action() {
		System.err.println("被代理类");
	}

}

class ProxyHandler implements InvocationHandler {
	Object obj;

	public Object blind(Object obj) {
		this.obj = obj;
		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Object returnVal = method.invoke(obj, args);
		return returnVal;
	}

}

public class TestProxy {
	public static void main(String[] args) {
		RealSubject realSubject = new RealSubject();
		ProxyHandler handler = new ProxyHandler();
		Subject subject = (Subject) handler.blind(realSubject);
		subject.action();
	}
}
